package actions.members;

import java.util.Map;

import veilingAware.UserAware;
import veilingDomein.User;
import veilingService.DBController;
import veilingService.IUserService;
import veilingService.ServiceProvider;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UpdateGegevens extends ActionSupport implements UserAware {
	private User user;
	private String naam;
	private String adres;
	private String woonplaats;
	private String username;
	private String password;
	private String email;
	private String passwordRepeat;
	private String emailRepeat;
	private IUserService ius = ServiceProvider.getUserService();

	@SuppressWarnings("rawtypes")
	private Map session;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getPasswordRepeat() {
		return passwordRepeat;
	}

	public void setPasswordRepeat(String passwordRepeat) {
		this.passwordRepeat = passwordRepeat;
	}

	public String getEmailRepeat() {
		return emailRepeat;
	}

	public void setEmailRepeat(String emailRepeat) {
		this.emailRepeat = emailRepeat;
	}

	public IUserService getIus() {
		return ius;
	}

	public void setIus(IUserService ius) {
		this.ius = ius;
	}

	public void setNaam(String naam) {
		this.naam = naam;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	DBController db = new DBController();

	public String execute() {
		session = ActionContext.getContext().getSession();
		try {
			user.setUsername(username);
			user.setNaam(naam);
			user.setPassword(password);
			user.setAdres(adres);
			user.setEmail(email);
			user.setWoonplaats(woonplaats);

			ius.updateUser(user);
			System.out.println(user);
			return SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return INPUT;
	}

	private String getEmail() {

		return email;
	}

	private String getPassword() {

		return password;
	}

	private String getNaam() {

		return naam;
	}

	private String getUsername() {

		return username;
	}

	public void validate() {

		username = username.trim();
		password = password.trim();
		naam = naam.trim();
		woonplaats = woonplaats.trim();
		adres = adres.trim();
		email = email.trim();

		if (username.length() == 0) {
			addFieldError("username", getText("username.required"));

		}

		if (naam.length() == 0) {
			addFieldError("naam", getText("Naam is verplicht."));
		} else
			for (int i = 0; i < naam.length(); i++) {
				if (Character.isDigit(naam.charAt(i))) {
					addFieldError("naam",
							getText("Alleen letters zijn toegestaan in namen"));
					break;
				}
			}

		if (woonplaats.length() == 0) {
			addFieldError("woonplaats", getText("Woonplaats is verplicht."));
		}

		if (adres.length() == 0) {
			addFieldError("adres", getText("Adres is verplicht."));
		}

		if (password.length() == 0) {
			addFieldError("wachtwoord", getText("Wachtwoord is verplicht."));
		}
		if (password.length() < 8) {
			addFieldError("password",
					getText("wachtwoord moet minimaal 8 tekens zijn"));
		}
		if (!password.matches(".*[0-9].*")) {
			addFieldError("password",
					getText("wachtwoord moet minimaal een cijfer bevatten!"));
		}
		if (!password.equals(passwordRepeat)) {
			addFieldError(
					"passwordRepeat",
					getText("De wachtwoorden die u invoerde komen niet overeen!"));
		}

		if (email.length() == 0) {
			addFieldError("email", getText("email.required"));
		}
		if (!email.equals(emailRepeat)) {
			addFieldError(
					"email",
					getText("De e-mailadressen die u invoerde komen niet overeen!"));
		}
	}

	public String getWoonplaats() {
		return woonplaats;
	}

	public void setWoonplaats(String woonplaats) {
		this.woonplaats = woonplaats;
	}

	public String getAdres() {
		return adres;
	}

	public void setAdres(String adres) {
		this.adres = adres;
	}

	@SuppressWarnings("rawtypes")
	public void setSession(Map session) {
		this.session = session;
	}

	@SuppressWarnings("rawtypes")
	public Map getSession() {
		return session;
	}

}
